/**
 * Created by wzq
 */
'use strict';

let Model = require('./models/checkout.js');
var sequelize = require('./sequelize.js');

var dao = {};
module.exports = dao;

dao.query = function*(where) {
  var retObj = yield Model.findAll({where: where});
  return retObj;
};

/**
 *
 {
   "vid":"123",
   "begin":"2015-01-01 00:00:00",
   "end":"2015-01-01 23:59:59",
   "wareid":111
 }
 */
dao.detail = function*(json) {
  var vid = json.vid;
  var wareid = json.wareid;
  var begin = new Date(json.begin).getTime() / 1000;
  var end = new Date(json.end).getTime() / 1000;
  var sql = "select od.vendorid as vid, od.wareid, ot.*   " +
    " from mh_order_ticket as ot, mh_order as od " +
    " where ot.order_id = od.orderid  and od.vendorid=%vid " +
    " and od.wareid=%wareid " +
    " and ot.consume_date between %begin and %end ";

  sql = sql
    .replace('%vid', vid)
    .replace('%wareid', wareid)
    .replace('%begin', begin)
    .replace('%end', end);

  console.log(sql);
  var resObj = yield sequelize.query(sql);
  var retObj = [];
  for (var item of resObj[0]) { // why not resObj[0] ?
    retObj.push(item);
  }
  return retObj;
};


dao.save = function*(json) {
  // TODO 检测是否已经有该条记录 vid,begin,end
  var commission = 0; // TODO 佣金计算
  console.log('dao save', json);
  var entity = yield Model.create({
    vid: json.vid,
    begin: new Date(json.begin).getTime() / 1000,
    end: new Date(json.end).getTime() / 1000,
    origin_money: json.origin_money,
    commission: commission,
    trans_money: json.origin_money - json.commission,
    status: 0
  });
  return entity;
};

dao.update = function*(where, update) {
  return yield Model.update(update, {where: where});
};

/**
 * 每月结算 结算时间必须是该月份的后一个月
 * @param year 年份
 * @param month 月份
 * @returns {{checkToday: Array, checkBefore: Array}}
 */
dao.checkOneMonth = function*(year, month) {

  // unix 时间戳
  var begin = new Date(year, month - 1, 1).getTime() / 1000; // 每月的第一秒
  var end = new Date(year, month, 1).getTime() / 1000 - 1; // 该月最后一秒 = 下个月第一天第一秒 -1
  var sql = "select od.vendorid as vid, " +
    " sum(ot.money) as origin_money " +
    " from mh_order_ticket as ot, mh_order as od " +
    " where ot.order_id = od.orderid " +
    " and ot.consume_date >=%begin " +
    " and ot.consume_date <=%end " +
    " group by vid ";
  sql = sql.replace('%begin', begin).replace('%end', end);
  console.log(sql);
  var resObj = yield sequelize.query(sql);

  var checkToday = [];
  var checkBefore = [];
  for (var item of resObj[0]) {
    var origin_money = item.origin_money;
    var commission = 0;// TODO
    // 防止之前已经结算过
    var hasSame = yield Model.find({where: {vid: item.vid, begin: begin}});
    if (hasSame) {
      checkBefore.push(hasSame);
      continue;
    }
    var saveResult = yield Model.create({
      vid: item.vid,
      begin: begin,
      end: end,
      origin_money: origin_money,
      commission: commission,
      trans_money: origin_money - commission,
      status: 0
    });
    checkToday.push(saveResult);
  }
  return {checkToday: checkToday, checkBefore: checkBefore}
};
